package com.jh.paymentchannel.service;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.jifu.DateTime;

import cn.com.sandpay.cashier.sdk.util.CertUtil;
import cn.com.sandpay.cashier.sdk.util.CryptoUtil;
import net.sf.json.JSONObject;
@Service
public class QuickPayService implements TopupRequest{
private static final Logger log = LoggerFactory.getLogger(QuickPayService.class);
	
		@Autowired
		Util util;
		private String key = "/product/deploy/fugui_sand_mid.pfx";
		private String pub_key = "/product/deploy/sand.cer";
		private String req_url = "https://cashier.sandpay.com.cn/fastPay/quickPay/index";
		
	@Override
	public Map<String, String> topupRequest(HttpServletRequest request,
			String amount, String ordercode, String orderdesc, String userid,
			String notifyurl, String returnurl, String channelParam)
			throws Exception {
		log.info("=======================进入杉德快捷====================");
		Map head = new HashMap();
		head.put("version", "1.0");
		head.put("method", "sandPay.fastPay.quickPay.index");
		head.put("productId", "00000016");
		head.put("accessType", "2");
		head.put("mid", "Z3809100");//@
		head.put("plMid", "P67356");//@
		head.put("channelType", "08");//@
		String reqTime = DateTime.getNow("yyyyMMddHHmmss");
		head.put("reqTime", reqTime);

		Map body = new HashMap();
		body.put("userId", String.valueOf(userid));
		body.put("orderCode", ordercode);
		body.put("orderTime", reqTime);
		String amt = new BigDecimal(amount).multiply(new BigDecimal("100")).toString();
		int num = amt.length();
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<(12-num);i++) {
			sb.append("0");
		}
		sb.append(amt);
		body.put("totalAmount", sb.toString());
		body.put("subject", "testTitle");//@
		body.put("body", "payOnLine");//@
		body.put("currencyCode", "156");
		body.put("notifyUrl", notifyurl);
		body.put("frontUrl", returnurl);
		body.put("clearCycle", "0");
		body.put("extend", "");
		JSONObject data = new JSONObject();
		data.put("head", head);
		data.put("body", body);
		log.info("======================start====================");
		CertUtil.init(pub_key, key, "521134");
		log.info("======================end====================");
		// 签名
		String reqSign = URLEncoder.encode(new String(Base64.encodeBase64(CryptoUtil
				.digitalSign(JSON.toJSONString(data).getBytes("UTF-8"), CertUtil.getPrivateKey(), "SHA1WithRSA"))), "UTF-8");
		log.info("======================sign-end====================");
		Map mapp = new HashMap();
		log.info("data===================="+data.toString());
		try {
			JSONObject obj = JSONObject.fromObject(mapp);
			obj.put("data", data);
			obj.put("sign", reqSign);
			obj.put("signType", "01");
			obj.put("extend", "");
			obj.put("charset", "utf-8");
			//obj.put("method", "post");
			/*String str = HttpRequest.sendPostJSON(req_url, data);*/
			log.info("obj===================="+obj.toString());
			String url = req_url + "?data="+URLEncoder.encode(data.toString(),"utf-8")+"&sign="+URLEncoder.encode(reqSign,"utf-8")+"&signType=01"+"&charset="+URLEncoder.encode("utf-8","utf-8")+"&extend="+URLEncoder.encode("\"\"","utf-8");
			log.info("url===================="+url);
			mapp.put("resp_code", "success");	
			mapp.put("channel_type", "quickpay");
			mapp.put("redirect_url", url);
			log.info("mapp===================="+mapp.toString());
			return mapp;
			//resp.getOutputStream().print(r.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		mapp.put("resp_code", "fail");
		return mapp;
	    
	}
	
}
